﻿using BootstrapBlazor.Components;
using Mapster;
using Microsoft.AspNetCore.Components;
using OnWork.DataCenter.BLL.DataCenter;
using OnWork.DataCenter.DAL.DataCenter;
using OnWork.DataCenter.Shared.Common;
using OnWork.DataCenter.Shared.Pages.DataSource;

namespace OnWork.DataCenter.Shared.Pages.DataStandard
{
    public partial class DataStandardConfigView : ComponentBaseDataCenter
    {
        /// <summary>
        /// 依赖注入 提示信息
        /// </summary>
        [Inject]
        protected DataStandardBLL dBConnBLL { get; set; }

        private Table<DataStandardConfigVO> table;

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private async Task<QueryData<DataStandardConfigVO>> QueryAsync(QueryPageOptions options)
        {
            var result = await dBConnBLL.QueryPagedListAsync(options.ToBaseQueryPageInput());
            //对象转换
            return result.ToQueryData<DataStandardConfigVO, OnWork.DataCenter.DAL.DataCenter.DataStandard>();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        private async Task<bool> OnDeleteAsync(IEnumerable<DataStandardConfigVO> item)
        {
            var result = await dBConnBLL.DeleteDataAsync(item.Select(x => x.ID).ToArray());

            return await ShowSqlReturnMsg(result);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="item"></param>
        /// <param name="changedType"></param>
        /// <returns></returns>
        private async Task<bool> OnSaveAsync(DataStandardConfigVO item, ItemChangedType changedType)
        {
            //RESTfulResult<object> result;
            if (changedType == ItemChangedType.Update) // 更新
            {
                var result = await dBConnBLL.UpdateDataAsync(item.Adapt<OnWork.DataCenter.DAL.DataCenter.DataStandard>());
                return await ShowSqlReturnMsg(result);
            }
            else //新增
            {
                var result = await dBConnBLL.AddDataAsync(item.Adapt<OnWork.DataCenter.DAL.DataCenter.DataStandard>());
                return await ShowSqlReturnMsg(result);
            }
        }


        /// <summary>
        /// 复制多个对象
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        private async Task CopyItemsAsync(IEnumerable<DataStandardConfigVO> items)
        {
            if (items.Count() > 0)
            {
                var result = await dBConnBLL.CopyDataAsync(items.Adapt<List<DAL.DataCenter.DataStandard>>());
                await ShowSqlReturnMsg(result);
                await table.QueryAsync();
            }
        }
    }
}